#!/usr/bin/env python
#coding=utf-8

'''
Created on 2009-2-26

@author: zarra
'''

from sqlalchemy import  Column, Integer, String,Unicode,UnicodeText, DateTime, ForeignKey
#from sqlalchemy.orm import relation, backref,eagerload,sessionmaker,scoped_session
import time
from datetime import  datetime



from model.expr_a._base import Base


 
class Patient(Base):
    __tablename__ = 'vw_brxx_all'
    
    expr_id = Column('expr_id',String(16),nullable=False)
    id      = Column('br_id'  ,String(16),primary_key=True)
    name    = Column('br_xm'  ,Unicode(32),nullable=False)
    sex     = Column('br_xb'  ,Unicode(16),nullable=False)
    ethnic  = Column('br_mz'  ,Unicode(8))
    phone   = Column('br_lxr_dh'  ,String(32))
    birthday= Column('br_sr'  ,String(8) ,nullable=False)
    flag    = Column('br_zt'  ,Unicode(8) ,nullable=False)
    ry_time = Column('br_rysj',DateTime,nullable=False)
    cy_time = Column('sy_cysj',String(16),nullable=False)
    zy_id   = Column('zy_id'  ,String(6),nullable=False)
    
    begin_bq=Column('br_rybq_id',String(16),nullable=False)
    bq = Column('sy_cybq_id',String(16),nullable=False)
    
    begin_department=Column('br_rykb_id',String(16),nullable=False)
    department = Column('sy_cykb_id',String(16),nullable=False)
    
    begin_bed  = Column('br_rych',String(16),nullable=False)
    bed        = Column('sy_cych',Unicode(16),nullable=False)
    
    def __init__(self,name,sex,birthday):
        self.name=name
        self.sex=sex
        self.birthday=birthday
    def __repr__(self):
        s='\n'.join( '%s = %s'%(field, str(getattr(self, field)) ) for field in dir(self) if not callable(getattr(self, field)) and (not field.startswith('_')) )
        return "<Metadata(\n%s\n)>" % (s)
    def __json__(self):
        return {'id'      : self.zy_id.strip(),\
                'name'    : self.name.strip(), \
                'bed'     : self.bed.strip(),  \
                'age'     : self.age,          \
                'ry_time' : self.ry_time,      \
                'cy_time' : self.cy_time,      \
                'birthday': self.birthday,     \
                'ethnic'  : self.ethnic,       \
                'phone'   : self.phone,       \
                'sex'     : self.sex.strip()}
    @property
    def age(self):
        now=datetime.now()
   
        birthday=datetime(*time.strptime(self.birthday,'%Y%m%d')[:3])
        
        days=(now-birthday).days
        u=u'日'
        n=0
        if days/365 >=1:
            n=days/365
            u=u'岁'
        elif days/30 >=1:
            n=days/30
            u=u'月'
        else:
            n=days
        return u"%d%s"%(n,u)
    
    
